package com.woniuxy.service.impl;

import com.woniuxy.dao.SaleCountDao;
import com.woniuxy.entity.CountEntity;
import com.woniuxy.entity.MonthSaleCount;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.SaleCountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

/**
 * @author Leimin
 * @date 2022/05/09 14:49
 **/
@Service
public class SaleCountServiceImpl implements SaleCountService {
    @Resource
    private SaleCountDao saleCountDao;
    
    @Override
    public ResponseEntity countByMonth() {
        //查询数据库
        List<MonthSaleCount> countList = saleCountDao.countByMonth();
        //由于无数据月份为空，需要按照顺序进行检查、填充
        TreeMap<String, MonthSaleCount> map = new TreeMap<>();
        for (MonthSaleCount count:countList) {
            map.put(count.getMonth(),count);
        }
        //获取开始时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String nowMonth = sdf.format(new Date()).toString();
        int nowMonthInt = Integer.parseInt(nowMonth);
        //开始填充
        for( int i=1;i<=12;i++){
            int fakeMonth=nowMonthInt-100+i;
            int realMonth=fakeMonth%100>12?fakeMonth+100-12:fakeMonth;
            if(!map.containsKey(realMonth+"")){
                map.put(realMonth+"",new MonthSaleCount(realMonth+"",0,new BigDecimal(0),0));
            }
        }
        return new ResponseEntity(200,"成功",map);
    }

    @Override
    public ResponseEntity countOrder_status() {
        List<CountEntity> list = saleCountDao.countOrder_status();
        return new ResponseEntity(200,"成功",list);
    }

    @Override
    public ResponseEntity countProduct_type() {
        List<CountEntity> list = saleCountDao.countProduct_type();
        return new ResponseEntity(200,"成功",list);
    }
}

